//
//  MSHomeFilterViewController.m
//  MSJD
//
//  Created by 箭军刘 on 2018/10/20.
//  Copyright © 2018年 xiangbo. All rights reserved.
//

#import "MSHomeFilterViewController.h"

#import "MSSearchViewController.h"

#import "MSFilterHeadView.h"
#import "MSFilterNavigationTitle.h"
#import "MSCollectDetailView.h"
#import "MSFilterHeadView.h"
#import "MSHotelDataSource.h"

@interface MSHomeFilterViewController ()

@property (nonatomic, strong) MSFilterNavigationTitle *naviTitle;
@property (nonatomic, strong) MSFilterHeadView *headView;
@property (nonatomic, strong) MSCollectDetailView *detailView;

@property (nonatomic, assign) NSInteger kind;
@property (nonatomic, assign) BOOL isFromSearchVC;
@property (nonatomic, copy) NSString *searchContent;
@property (nonatomic, copy) NSDictionary *filterInfo; // 筛选项内容

@property (nonatomic, strong) NSMutableDictionary *filterParam;

@end

@implementation MSHomeFilterViewController

+ (instancetype)loadHomeFilterViewControllerWithKind:(NSInteger)kind
                                      IsfromSearchVC:(BOOL)isFromSearchVC
                                       searchContent:(NSString *)searchContent
                                          filterInfo:(nonnull NSDictionary *)filterInfo {
    MSHomeFilterViewController *homeFilterVC = [[MSHomeFilterViewController alloc] init];
    homeFilterVC.kind = kind;
    homeFilterVC.isFromSearchVC = isFromSearchVC;
    homeFilterVC.searchContent = searchContent;
    homeFilterVC.filterInfo = filterInfo;
    return homeFilterVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = rgba(242.0, 242.0, 242.0, 1.0);
    [self initSubViews];
    
    if (![MSSafeObject isNull:self.searchContent]) {
        [self.naviTitle changeNavititle:self.searchContent];
    }
    [self.filterParam setObject:@(self.kind) forKey:@"kid"];
    [self.filterParam setObject:[MSSafeObject stringInfo:self.searchContent] forKey:@"keyword"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.headView) {
        [self.headView hiddFilterView];
    }
}

#pragma mark - Init Method

- (void)initSubViews {
    // 添加自定义导航条控件
    CGFloat naviHeight = 44.0;
    CGFloat naviY = MS_STATUS_HEIGHT;
    [self.view addSubview:self.naviTitle];
    [self.naviTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(naviY);
        make.left.right.offset(0);
        make.height.offset(naviHeight);
    }];
    // 添加筛选控件
    CGFloat headViewHeight = 36.0;
    [self.view addSubview:self.headView];
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(self.naviTitle.mas_bottom);
        make.height.offset(headViewHeight);
    }];
    // 筛选控件
    CGFloat detailViewHeight = MS_SCREEN_HEIGHT - headViewHeight - naviHeight - MS_STATUS_HEIGHT;
    [self.view addSubview:self.detailView];
    [self.detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(detailViewHeight);
    }];

    [self loadFilterParamData];
}
/** 加载筛选项 */
- (void)loadFilterParamData {
    if (![MSSafeObject isNull:self.filterInfo]) {
        return;
    }
    // 查询筛选项的内容
    _WEAKSELF
    [MSHotelDataSource queryFilterListWithKind:self.kind success:^(NSDictionary * _Nonnull responseInfo) {
        [weakSelf.headView refreshWithFilterInfo:responseInfo[@"data"]];
        // 根据筛选项内容默认项查询筛选后的数据
        [weakSelf.detailView refreshTabelViewWithNewParam:self.filterParam.copy filterDic:@{}];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Event Response
/** 返回按钮点击 */
- (void)backButtonClick {
    [self.navigationController popViewControllerAnimated:YES];
}
/** 搜索按钮点击 */
- (void)searchButtonClick {
    MSSearchViewController *searchVC = [MSSearchViewController loadSearchViewControllerWithKind:self.kind title:self.searchContent];
    // 搜索某个内容
    _WEAKSELF
    searchVC.SearchButtonClickBlock = ^(NSString * _Nonnull title) {
        // 选择了某个搜索内容
        weakSelf.searchContent = title;
        [weakSelf.naviTitle changeNavititle:title];
        // 重置已选中的筛选数据
        if (weakSelf.headView) {
            [weakSelf.headView reloatFilterData];
        }
        [weakSelf.filterParam setObject:title forKey:@"keyword"];
        [weakSelf.detailView refreshTabelViewWithNewParam:self.filterParam.copy filterDic:@{}];
    };
    [self.navigationController pushViewController:searchVC animated:NO];
}

#pragma mark - Private Method
/** 地图按钮点击 */
- (void)mapButtonClick {
    // FIXME:
}

- (void)changeHeadViewOffsetWithIsShow:(BOOL)isShow {
    if (isShow) {
        // 展示
        [self.headView startShowFilterView];
    }
}

- (void)selectNewFilter:(NSDictionary *)filterDic {
    // 重新请求筛选数据
    [self.detailView refreshTabelViewWithNewParam:self.filterParam.copy filterDic:filterDic];
}

#pragma mark - Getters

- (MSFilterNavigationTitle *)naviTitle {
    if (!_naviTitle) {
        CGFloat width = MS_SCREEN_WIDTH - 2 * 50;
        CGFloat height = 32;
        CGFloat naviTitleY = (44 - 32.0) / 2.0;
        _naviTitle = [MSFilterNavigationTitle loadSearchNavigationTitle];
        [_naviTitle setFrame:CGRectMake(60, naviTitleY, width, height)];
        _naviTitle.layer.cornerRadius = height / 2.0;
        
        _WEAKSELF
        _naviTitle.BackButtonClickBlock = ^{
            [weakSelf backButtonClick];
        };
        _naviTitle.SearchButtonClickBlock = ^{
            [weakSelf searchButtonClick];
        };
        _naviTitle.MapButtonClickBlock = ^{
            [weakSelf mapButtonClick];
        };
    }
    return _naviTitle;
}

- (MSFilterHeadView *)headView {
    if (!_headView) {
        _headView = [MSFilterHeadView loadFilterHeadViewWithIsHotel:NO];
        _WEAKSELF
        _headView.ShowFilterViewBlock = ^{
            [weakSelf changeHeadViewOffsetWithIsShow:YES];
        };
        _headView.HiddenFilterBlock = ^{
            [weakSelf changeHeadViewOffsetWithIsShow:NO];
        };
        _headView.ChangeFilterBlock = ^(NSDictionary * _Nonnull filterDic) {
            [weakSelf selectNewFilter:filterDic];
        };
    }
    return _headView;
}

- (MSCollectDetailView *)detailView {
    if (!_detailView) {
        _detailView = [[MSCollectDetailView alloc] initWithFrame:CGRectZero type:MSCollectDetailViewTypeFilter];
    }
    return _detailView;
}

- (NSMutableDictionary *)filterParam {
    if (!_filterParam) {
        _filterParam = [NSMutableDictionary dictionary];
    }
    return _filterParam;
}

#pragma mark -  Memory Method

- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end
